/***************************************************************************
 *   Copyright (C) 2007 by Eduardo Robles Elvira   *
 *   edulix@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef EVENTSMODEL_H
#define EVENTSMODEL_H

#include <QAbstractTableModel>
#include <QThread>
#include <QVector>
#include <libnetfilter_conntrack/libnetfilter_conntrack.h>
#include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h>
#include "common.h"

class QDomElement;
class QDomNode;
class QVariant;
class QModelIndex;
class QItemSelectionModel;
class KonntrackItem;

class EventsModel : public QAbstractTableModel
{
	Q_OBJECT

	public:
		EventsModel(QObject *parent = 0);
		~EventsModel();

		QVariant data(const QModelIndex &index, int role, bool translated) const;
		QVariant data(const QModelIndex &index, int role) const;
		QVariant headerData(int section, Qt::Orientation orientation,
				    int role = Qt::DisplayRole) const;
		int rowCount(const QModelIndex &parent = QModelIndex()) const;
		int columnCount(const QModelIndex &parent = QModelIndex()) const;
		
		class EventsThread : public QThread {
			public:
				EventsThread(EventsModel *parent);
				~EventsThread();
				virtual void run();
				static int event_cb(enum nf_conntrack_msg_type type, struct nf_conntrack *ct, void *data);
			protected:
				EventsModel *parent;
				struct nfct_handle *h;
		};
	
	public slots:
		void  ipNameResolved();
		void  clearEvents();
	
	protected:
		/*$PROTECTED_FUNCTIONS$*/
		void listConntrack(char string[], nf_conntrack *ct);
		void processMeta(QDomElement &meta);
		void setColText(int col, QDomNode &node);
		void setHandle(struct nfct_handle *h) { this->h = h; }
	protected:
		/*$PRIVATE_VARIABLES$*/
		u_int8_t family;
		struct nfct_handle *h;
		EventsThread et;
		QVector<KonntrackItem*> items;
};

#endif
